
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.query;

import java.util.Hashtable;
import java.util.Vector;

//import com.mysql.jdbc.ResultSet;

import utility.CapitalChar;
import utility.ConvertToLower;
import utility.Splitstring;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import java.sql.ResultSet;

import com.mysql.jdbc.ResultSetMetaData;

/**
 * @author     wiki
 */
public class Fieldlist implements Queryinterface 
{
	String tablename;
	String object;
	String[] buffer;
	String[] queryBuffer;
	String key="no$";
	String value="no$";  
	String packagename="query.fieldlist.";
	String procRes[] = null;
	
	String FIELD="field";
	/**
	 * @uml.property  name="reportname"
	 */
	String Reportname;
	String Query;
	String tid;
	/**
	 * @uml.property  name="query"
	 */
	Hashtable<Object, Object> QueryMap = new Hashtable<Object, Object>();
	Hashtable<Object, Object> ReportMap =new Hashtable<Object, Object>();
	String reporton;
	GetIdName gid = new GetIdName();
	Vector<Object> Fieldlist =new Vector<Object>();
	int count;
	//PoolLogger pl;
	String TYPE="type";
	String strProcess = "process";
	String strvalidation = "validation";
	String stradd = "add"; 
	String strnull = "null";
	
	public Fieldlist() {
		// logger
		//pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public void queryProcess(){
		try
		{
			query.fieldlist.Fieldlist fieldlist;
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			fieldlist = (query.fieldlist.Fieldlist)C.newInstance();
			fieldlist.initFieldList();
			fieldlist.setFieldVector(Fieldlist);
			fieldlist.setQuerybuffer(queryBuffer);
			fieldlist.setQueryCount(count);
			fieldlist.setQuery(Query);
			fieldlist.setbuffer(buffer);
			fieldlist.updateReport();
			buffer=fieldlist.getbuffer();
			Query = fieldlist.getQuery();		
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
		
			e.printStackTrace();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public void getResult() 
	{
		try 
		{
			query.fieldlist.Fieldlist fieldlist;
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			fieldlist = (query.fieldlist.Fieldlist)C.newInstance();
			fieldlist.initFieldList();
			fieldlist.setFieldVector(Fieldlist);
			fieldlist.setQuery(Query);
			fieldlist.setQueryCount(count);
			fieldlist.setbuffer(buffer);
			fieldlist.getResult();
			buffer=fieldlist.getbuffer();
			Query = fieldlist.getQuery();
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
		
			e.printStackTrace();
		}
		catch (IllegalAccessException e) 
		{
			e.printStackTrace();
		}
	}
	
	

	
	public void initializeData() 
	{
		try {
			Hashtable<Object, Object> table = new Hashtable<Object, Object>();
			table.put("mid", object);
			table.put("pid", FIELD);
			table.put("td", "null");
			RecordSearch rs=new RecordSearch();
			rs.setConditionMap(table);
			String line[] =rs.getArrayFromResultSet();
			for(int i=0;i<line.length;i++)
			{
				String splitline[] = line[i].split("\t");
				table.clear();
				table.put("mid", splitline[Column.pv_index-1]);
				table.put("td","null");
				rs.setConditionMap(table);
				String result[] = rs.getArrayFromResultSet();
				Hashtable<Object, Object> fieldproperty = new Hashtable<Object, Object>();
				for(int j=0;j<result.length;j++)
				{	
					String splitresult[] =result[j].split("\t");
					String value = splitresult[Column.pv_index-1];
					if(value.equals("0"))
						value =  splitresult[Column.vt_index-1];				
					fieldproperty.put(gid.getItem(splitresult[Column.pid_index-1]), value);
				}
				fieldproperty = ConvertToLower.convertHashKey(fieldproperty);
				Fieldlist.add(fieldproperty);
			}
			
			try
			{
				query.condition.typereport.Typereport type; 
				Class C = Class.forName("query.condition.typereport."+CapitalChar.makeFirstCharCapital(this.typeofreport.trim()));
				type = (query.condition.typereport.Typereport )C.newInstance();
				type.setData("", value,ReportMap);
				Fieldlist.addAll(type.getFieldVector());
			}
			catch (ClassNotFoundException e) 
			{	
			}
			catch (InstantiationException e) 
			{			
				e.printStackTrace();
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	public void initPool() 
	{
		
		FIELD = gid.getId(FIELD);
		TYPE = gid.getId(TYPE);
		strProcess = gid.getId(strProcess);
		strvalidation = gid.getId(strvalidation);
		stradd = gid.getId(stradd);
	}

	
	
	/**
	 * @param object
	 * @uml.property  name="object"
	 */
	
	
	public void setObject(String object) 
	{
		this.object = object;
	}

	public void setQuerybuffer(String[] Querybuffer) 
	{
		queryBuffer = Querybuffer;
	}

	public void setbuffer(String[] Querybuffer) 
	{
		this.buffer = Querybuffer;
	}

	public void settablename(String tablename) 
	{	
		this.tablename = tablename;
	}

	public String gettablename() 
	{
		return tablename;
	}

	public String[] getbuffer() 
	{
		return buffer;
	}
	public String[] getQuerybuffer() 
	{
			return queryBuffer;
	}

	/**
	 * @param key
	 * @uml.property  name="key"
	 */
	public void setKey(String key) 
	{
		this.key = key;
	}

	/**
	 * @param value
	 * @uml.property  name="value"
	 */
	public void setValue(String value) 
	{
		this.value= value;
	}
	
	public String getkey() 
	{
		return key;
	}


	public String getvalue() 
	{
		return value;
	}

	public String getQueryName() 
	{
		return null;
	}

	public void setQueryName(String QueryName) 
	{
		
	}
	

	/**
	 * @return
	 * @uml.property  name="query"
	 */
	public Hashtable<Object, Object> getQueryMap() 
	{
		return QueryMap;
	}

	/**
	 * @param QueryMap
	 * @uml.property  name="query"
	 */
	public void setQueryMap(Hashtable<Object, Object> QueryMap) 
	{
		this.QueryMap =  QueryMap;
	}

	public String gettid() 
	{
		return tid;
	}

	public void settid(String tid) 
	{
		this.tid =tid;	
	}

	@SuppressWarnings("unchecked")
	public void addReport() 
	{
		try
		{
			query.fieldlist.Fieldlist fieldlist;
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			fieldlist = (query.fieldlist.Fieldlist)C.newInstance();
			fieldlist.initFieldList();
			fieldlist.setFieldVector(Fieldlist);
			fieldlist.setQuerybuffer(queryBuffer);
			fieldlist.setQueryCount(count);
			fieldlist.setQuery(Query);
			fieldlist.setbuffer(buffer);
			fieldlist.updateReport();
			buffer=fieldlist.getbuffer();
			Query = fieldlist.getQuery();		
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
		
			e.printStackTrace();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void updateReport() 
	{
		try
		{
			query.fieldlist.Fieldlist fieldlist;
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			fieldlist = (query.fieldlist.Fieldlist)C.newInstance();
			fieldlist.initFieldList();
			fieldlist.setFieldVector(Fieldlist);
			fieldlist.setQuerybuffer(queryBuffer);
			fieldlist.setQueryCount(count);
			fieldlist.setQuery(Query);
			fieldlist.setbuffer(buffer);
			fieldlist.updateReport();
			buffer=fieldlist.getbuffer();
			Query = fieldlist.getQuery();		
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
		
			e.printStackTrace();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	/**
	 * @return
	 * @uml.property  name="reportname"
	 */
	public String getReportname() 
	{
		return Reportname;
	}

	public void setReportName(String Reportname) 
	{
		this.Reportname = Reportname;
	}
	
	

	public void setreporton(String reporton) 
	{
		this.reporton = reporton;
	}

	public void setReportmap(Hashtable<Object, Object> ReportMap) 
	{
		this.ReportMap.putAll(ReportMap);
	}
	String typeofreport;
	public void settypeofreport(String typeofreport) 
	{ 
		this.typeofreport = typeofreport;
	}
	public void setViewObject(String Object,char ch)
	{
		Splitstring sp =new Splitstring();
	    String splitobject[] = sp.split(Object,Character.toString(ch));
	    for(int i=0;i<splitobject.length;i++)
	    {
	    	Hashtable<Object, Object> table =new Hashtable<Object, Object>();
	    	table.put("field",splitobject[i]);
	    	Fieldlist.add(table);
	    }
	    
	    
	    
	}

	public void setViewObject(Vector<Object> PropertyMap) 
	{
	 this.Fieldlist.addAll(PropertyMap);	
	}
	

	/**
	 * @param Query
	 * @uml.property  name="query"
	 */
	public void setQuery(String Query)
	{
		this.Query = Query;
	}
	
	
	
	
	public String[] executeQuery(){   ////// temperory for testing.
		
		ResultSet rs  = null ;
		rs = DBManager.getSelect(Query);
		
		
		Hashtable<Object, Object> tempHash = new Hashtable<Object, Object>();
		
		String strType = "Type";
		
		
		String strres[] = null;
		
		try{
			RecordSearch rsearch = new RecordSearch();
			rs.next();
			
			String mid=rs.getString("mid");			
			tempHash.put("mid", mid);
			tempHash.put("pid", TYPE);
			tempHash.put("td", strnull);
			rsearch.setConditionMap(tempHash);			
			
			String strpv = rsearch.getArrayFromResultSet()[0].toString().split("\t")[2];
			 
			
			
			RecordSearch rsearch1 = new RecordSearch();
					
			tempHash.clear();
			tempHash.put("mid",strpv);
			tempHash.put("pid", strProcess);
			tempHash.put("td", "null");
		
			rsearch1.setConditionMap(tempHash);
			
			strres = rsearch1.getArrayFromResultSet();
			
			String itemname[] = null;
			
			String returnob;
			
			 
			for(int i = 0 ; i < strres.length ; i++){
				RecordSearch rs1 = new RecordSearch();
				tempHash.clear();
				strres[i] = strres[i].toString().split("\t")[2];
				
			}
			
			for(int i = 0 ; i < strres.length ; i++)
			{
				RecordSearch rs1 = new RecordSearch();
				tempHash.put("mid",strres[i]);
				tempHash.put("pid", stradd);
				//tempHash.put("pid", TYPE);
				rs1.setConditionMap(tempHash);
				procRes = rs1.getArrayFromResultSet();
			}
			
			
			for(int j=0; j < procRes.length; j++){
				procRes[j] = procRes[j].split("\t")[2];
			}
			
			
			//procRes = null;
				
				
/*
 
  
				if(gid.getItem(returnob = procRes[0].split("\t")[2]).equals("validation")){
					System.out.println(returnob);
					break;
					
				}

				
*/				
				
			
		//	System.out.println(strres.toString());
		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	
		return procRes;
			
		
	}
	
	
	
	
	
	/**
	 * @return
	 * @uml.property  name="query"
	 */
	public String getQuery() 
	{
		return Query;
	}

	
	public int getcount() {
		
		return 0;
	}

	
	/**
	 * @param count
	 * @uml.property  name="count"
	 */
	public void setCount(int count) 
	{	
		this.count =  count;
	}

	
	public void setMaxcount(int count) {
		
		
	}

	
	@SuppressWarnings("unchecked")
	public void buildQuery() 
	{
		try 
		{
			query.fieldlist.Fieldlist fieldlist;
			Class C = Class.forName(packagename+CapitalChar.makeFirstCharCapital(tablename.trim()));
			fieldlist = (query.fieldlist.Fieldlist)C.newInstance();
			fieldlist.initFieldList();
			fieldlist.setFieldVector(Fieldlist);
			fieldlist.setQuery(Query);
			fieldlist.setbuffer(buffer);
			fieldlist.setQueryCount(count);
			fieldlist.updateReport();
			Query = fieldlist.getQuery();
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
		
			e.printStackTrace();
		}
		catch (IllegalAccessException e) 
		{
			e.printStackTrace();
		}
	}
	public void setConditionMap(Vector<Object> QueryMap)
	{
		
	}
	public void setProcess(String process)
	{
		
	}
}
